$(function(){
	var page=1;
	var main=[[ //表头
		 {type:'checkbox', fixed: 'left'}
        ,{field: 'number', title: '车牌号',  sort: true, fixed: 'left'}
        ,{field: 'name', title: '品牌',  sort: true}
        ,{field: 'car_type_name', title: '车型'} 
        ,{field: 'company_name',width:300, title: '所属公司'}
        ,{field: 'register_time', width:200,title: '注册时间', sort: true}
        ,{field:'state2', title:'状态'}
        ,{ title: '操作',width:200, toolbar:'#operation',fixed: 'right'}
    ]];
    $('#more').on('click',function(){
    	if($('#showmore').css('display')=='none'){
    		$('#showmore').css('display','block')
    	}else{
    		$('#showmore').css('display','none')
    	}
    });
    var data={};
    data.page=page;
    data.pagenum=10;
    var where_json={}
    {if $Think.get.number}
		where_json.number=['like','%{$Think.get.number}%']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	{if $Think.get.car_type_name}
		where_json.car_type_name=['like','%{$Think.get.car_type_name}%']
	{/if}
	{if $Think.get.is_check}
		where_json.is_check=['in','{$Think.get.is_check}']
	{/if}
	
	data.where_json=JSON.stringify(where_json)
    ajaxcar(data)
	function ajaxcar(data){
		Load()
		$.ajax({
			type:'post',
			data:data,
			url:'{:url("/adminapi/Car/carList")}',
			success:function(res){
				Load(1)
				if(res.code==1){
					console.log(res)
					for(var i=0;i<res.data.length;i++){
						if(res.data[i].state==1){
							res.data[i].state2='启用'
						}else{
							res.data[i].state2='禁用'
						}
					}
					
					  var table = layui.table
					  ,form = layui.form;
					  //展示已知数据
						table.render({
						  	elem: '#car'
						    ,cellMinWidth: 80
						    ,cols: main
							,data: res.data
							,even: true
						});
						$('.pagination').html(pagechang1(res.count, 10, page))
						table.on('tool(test)', function(obj){
							  var data = obj.data; //获得当前行数据
							  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
							  var tr = obj.tr; //获得当前行 tr 的DOM对象
							 
							  if(layEvent === 'detail'){ //查看
							  	
							  	window.location.href = "{:url('/admin/car/detail')}?car_id="+data.car_id+'&remote=1'
							  } else if(layEvent === 'del'){ //删除
							    layer.confirm('是否删除', function(index){
							    	Load()
							    	 $.ajax({
									method: "POST",
									url: "{:url('/admin/car/delete')}",
									data: {
										id:data.car_id
									},
									success: function(res) {
										if(res.code == 1) {
											ajaxcar(data)
											 layer.close(index);
											 Load(1)
											
										} else {
											 layer.close(index);
											 Load(1)
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
			
									},
									error:function(){
										Load(1)
										Err('请求失败')
									}
								});
							      layer.close(index);
							    });
							  } else if(layEvent === 'edit'){ //编辑
							    //do something
							   	window.location.href = " {:url('/admin/car/edit')}?car_id="+data.car_id+"&page="+page+'&remote=1'
							    //同步更新缓存对应的值
							   
							  } else if(layEvent === 'forbidden'){
							  	layer.confirm('是否禁用', function(index){
							    	Load()
							    	 $.ajax({
									method: "POST",
									url: "{:url('/admin/car/state')}",
									data: {
										id:data.car_id,
										state: 9
									},
									success: function(res) {
										if(res.code == 1) {
											ajaxcar(data)
											 layer.close(index);
											 Load(1)
											
										} else {
											 layer.close(index);
											 Load(1)
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
			
									},
									error:function(){
										Load(1)
										Err('请求失败')
									}
								});
							      layer.close(index);
							    });
							  }else if(layEvent === 'startusing'){
							  	layer.confirm('是否启用', function(index){
							    	Load()
							    	 $.ajax({
									method: "POST",
									url: "{:url('/admin/car/state')}",
									data: {
										id:data.car_id,
										state: 1
									},
									success: function(res) {
										if(res.code == 1) {
											ajaxcar(data)
											 layer.close(index);
											 Load(1)
											
										} else {
											 layer.close(index);
											 Load(1)
											Err(res.msg ? res.msg : '操作失败，稍候再试');
										}
			
									},
									error:function(){
										Load(1)
										Err('请求失败')
									}
								});
							      layer.close(index);
							    });
							  }
						});
						form.on('switch(state)', function(obj){
							
							console.log(obj.elem.checked)
							if(obj.elem.checked){
								
							}else{
								
							}
						});
					
					
					$('.laquo').on('click',function(){
						console.log(data)
						page-=1;
						data.page=page
						ajaxcar(data)
					})
					$('.pageli').on('click',function(){
						console.log(data)
						page=$(this).html()
						data.page=page
						ajaxcar(data)
					})
					//下一页
					$('.raquo').on('click',function(){
						page+=1;
						data.page=page
						ajaxcar(data)
					});
				}else{
					Err(res.msg)
				}
			},
			error:function(){
				Err('请求失败')
			}
		})
	}
	
})
